package com.kyyc.luff.core.exception;

import com.kyyc.luff.core.result.ResultEnum;
import com.kyyc.luff.core.result.ResultView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.nio.file.AccessDeniedException;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 全局异常.
     *
     * @param e the e
     * @return ResultView
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultView exception(Exception e) {
        log.error("全局异常信息 ex={}", e.getMessage(), e);
        return ResultView.error(e.getMessage());
    }

    /**
     * validation Exception
     *
     * @param exception
     * @return ResultView
     */
    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultView bodyValidExceptionHandler(MethodArgumentNotValidException exception) {
        List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
        log.warn(fieldErrors.get(0).getDefaultMessage());
        return ResultView.error(fieldErrors.get(0).getDefaultMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ConstraintViolationException.class)
    public ResultView validatorException(final ConstraintViolationException e) {
        log.error("验证实体异常 => {}", e.getMessage());
        final String msg = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(","));
        return ResultView.error(msg);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({SQLException.class, DataAccessException.class})
    public ResultView databaseException(final Throwable e) {
        log.error("数据库异常 => {}", e.getMessage());
        return ResultView.error("database error");
    }

    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler({AccessDeniedException.class})
    public ResultView userException(final Throwable e) {
        log.error("用户异常 => {}", e.getMessage());
        return ResultView.error(e.getMessage());
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler({NoHandlerFoundException.class})
    public ResultView apiNotFound(final Throwable e) {
        log.error("API 不存在 => {}", e.getMessage());
        return ResultView.error("API not existed");
    }

    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultView methodNotSupport(final Throwable e) {
        log.error("方法异常 => {}", e.getMessage());
        return ResultView.error("method error");
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResultView globalException(final Throwable e) {
        log.error("全局异常 => {}", e.getMessage());
        return ResultView.error(e.getMessage());
    }
}
